package com.hjm.admin.moule.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;

import com.afollestad.materialdialogs.MaterialDialog;
import com.allen.library.RxHttpUtils;
import com.allen.library.download.DownloadObserver;
import com.allen.library.utils.ToastUtils;
import com.daimajia.numberprogressbar.NumberProgressBar;
import com.hjm.admin.moule.R;
import com.hjm.admin.moule.model.AppUpdateBean;

import java.io.File;

import io.reactivex.disposables.Disposable;

public class AppUpdateUtils {
    public static AppUpdateUtils mAppUpdateUtils;
    Disposable mDisposable;

    public static AppUpdateUtils getInstance() {
        if (mAppUpdateUtils == null) {
            mAppUpdateUtils = new AppUpdateUtils();
        }
        return mAppUpdateUtils;
    }

    public void appUpdate(Context context, AppUpdateBean appUpdateBean) {
        new MaterialDialog.Builder(context)
                .title("版本更新")
                .content("是否更新到最新版本")
                .positiveText("立即更新")
                .onPositive((dialog, which) -> {
                    dialog.dismiss();
                    updateDownLoad(context, appUpdateBean.getDownloadurl());
                }).negativeText("取消")
                .onNegative((dialog, which) -> {
                    dialog.dismiss();
                }).show();
    }

    private void updateDownLoad(Context context, String downloadUrl) {
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.layout_app_update, false)
                .title("下载中...")
                .negativeText("取消下载")
                .onNegative((dialog12, which) -> {
                    if (mDisposable != null) {
                        mDisposable.dispose();
                    }
                    ToastUtils.showToast("去下更新");
                    dialog12.dismiss();
                })
                .build();
        dialog.setOnDismissListener(dialog1 -> {
            if (mDisposable != null) {
                mDisposable.dispose();
            }
            ToastUtils.showToast("取消更新");
            dialog1.dismiss();
        });
        NumberProgressBar npb_download = dialog.getCustomView().findViewById(R.id.npb_download);
        dialog.show();
        RxHttpUtils.downloadFile(downloadUrl)
                .subscribe(new DownloadObserver("HJMEmployees.apk") {
                    @Override
                    protected void getDisposable(Disposable disposable) {
                        mDisposable = disposable;
                    }

                    @Override
                    protected void onError(String s) {

                    }

                    @Override
                    protected void onSuccess(long l, long l1, float v, boolean b, String s) {
                        if (b) {
                            dialog.dismiss();
                            Intent intent = new Intent(Intent.ACTION_VIEW);
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                Uri contentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", new File(s));

                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                                intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
                            } else {
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.setDataAndType(Uri.fromFile(new File(s)), "application/vnd.android.package-archive");
                            }
                            context.startActivity(intent);
                        } else {
                            npb_download.setProgress((int) v);
                        }
                    }
                });
    }
}
